package org.ucas.faker.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.ucas.faker.web.dto.CarryDto;
import org.ucas.faker.web.dto.ListDto;
import org.ucas.faker.web.dto.ResponseDto;
import org.ucas.faker.web.entity.Application;
import org.ucas.faker.web.entity.Depot;
import org.ucas.faker.web.model.KeywordNode;
import org.ucas.faker.web.service.ApplicationService;
import org.ucas.faker.web.service.graph.SpecialKeywordService;
import org.ucas.faker.web.utils.feature.FeatureGenTask;

import java.util.List;

@RestController
@SuppressWarnings("unchecked")
public class ApplicationController extends BaseController {


	 @Autowired
	  private ApplicationService applicationService;
	 @Autowired
	  private SpecialKeywordService specialKeywordService;

  @Autowired
  private FeatureGenTask task;
  
  @RequestMapping(value = "/applications/{code}", method = RequestMethod.GET)
  public CarryDto<Application> getApplication(@PathVariable String code) {
    Application application = applicationService.load(code);
    return CarryDto.of(application);
  }


  @RequestMapping(value = "/applications/list", method = RequestMethod.GET)
  public ListDto<Application> listApplications(@RequestParam(value = "category", required = false) String category) {
    List<Application> applications = applicationService.list(category);
    return ListDto.of(applications);
  }
  @RequestMapping(value = "/applications/prepare", method = RequestMethod.POST)
  public ResponseDto prepare(@RequestParam(value = "tree_id", required = false) Long treeId) {

	  if(treeId!=null){
		  List<KeywordNode> tree = specialKeywordService.getSpecialKeywordTree(treeId);
		  //System.out.println(currentDepotId());
		  currentTreeId(treeId);
		  System.out.println("currentDepotId-----------------"+currentDepotId());
		  task.syncGenFeature(currentDepotId(),tree);
	  }
	  else{
		  task.syncGenFeature(currentDepotId());
	  }

    //task.syncGenFeature(currentDepotId());
    return ResponseDto.OK;
  }
  
  
}
